<template>
  <div class="page">
    <!-- 搜索区域 开始 -->
    <div class="el_card_box">
      <el-form :inline="true" size="small" :model="formInline" class="search_form">
        <el-form-item label="是否上首页：">
          <el-select v-model="formInline.isFeatured" filterable>
            <el-option label="是" :value="true"></el-option>
            <el-option label="否" :value="false"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上架状态：">
          <el-select v-model="formInline.isPublished" filterable>
            <el-option label="是" :value="true"></el-option>
            <el-option label="否" :value="false"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关键词：">
          <el-input v-model="formInline.key" placeholder="文章名称"></el-input>
        </el-form-item>
      </el-form>
      <div class="search_btn_box">
        <el-button type="primary" size="small" @click="getList">筛选</el-button>
        <el-button type="default" size="small" @click="rest">清空筛选</el-button>
      </div>
    </div>
    <!-- 搜索区域 end -->

    <!-- 表格 开始 -->
    <div class="table_box">
      <!-- 操作区域 开始 -->
      <div class="action_box">
        <el-button type="primary" size="small" @click="add()">新增文章</el-button>
      </div>
      <!-- 操作区域 end -->

      <el-table ref="elTable" class="el_table" size="small" :data="tableData">
        <el-table-column prop="articleName" label="文章名称" />
        <el-table-column prop="isFeatured" label="是否上首页" show-overflow-tooltip>
          <template slot-scope="{row}">{{ row.isFeatured == 1?'是':'否' }}</template>
        </el-table-column>
        <el-table-column prop="sortTime" label="排序时间" show-overflow-tooltip />
        <el-table-column prop="isPublished" label="上架状态" show-overflow-tooltip>
          <template slot-scope="{row}">{{ row.isPublished == 1?'是':'否' }}</template>
        </el-table-column>
        <el-table-column prop="createdAt" label="添加时间" show-overflow-tooltip />
        <el-table-column prop="addedBy" label="添加人" show-overflow-tooltip />
        <el-table-column prop="address" label="操作" width="200px" align="center">
          <template slot-scope="{row}">
            <el-link type="primary" class="el_link" :underline="false" :id="row.id" @click="add(row.id)">编辑</el-link>
            <el-link type="danger" class="el_link" :underline="false" :id="row.id" @click="remove(row)">删除</el-link>
            <el-link type="danger" class="el_link" :underline="false" :id="row.id" @click="update(row)">{{row.isPublished?'下架':'上架'}}</el-link>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination_list">
        <el-pagination background layout="sizes, total,prev, pager, next, jumper" :total="pageTotal" :page-size="pageSize" :page-sizes="pageSizes" :current-page.sync="pageNumber" @current-change="getList" @size-change="changePageSize" />
      </div>
    </div>
    <!-- 表格 end -->
  </div>
</template>
    
    <script>
import { showToast, showModal, deepClone } from "@/common/util";
import { GET_ARTICLE_LIST, DELETE_ARTICLE_INFO,UPDATE_ARTICLE_INFO } from "@/http/interface";
export default {
  data() {
    return {
      formInline: {},
      tableData: [],
      pageTotal: 0,
      pageNumber: 1,
      pageSizes: [10, 20, 50, 100],
      pageSize: 20,
      loading: false,
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    /** 重置 */
    rest() {
      this.formInline = {};
      this.orderTime = [];
      this.getList();
    },

    /** 每页展示 */
    changePageSize(e) {
      this.pageSize = e;
      this.getList();
      //this.getHospital();
    },

    /** 获取参数 */
    getParams() {
      let oParams = JSON.parse(JSON.stringify(this.formInline));
      oParams.current = this.pageNumber;
      oParams.size = this.pageSize;
      return oParams;
    },

    /** 获取列表 */
    getList() {
      let _this = this;
      let oParams = this.getParams();
      GET_ARTICLE_LIST(oParams).then((res) => {
        let obj = res.data || {};
        _this.tableData = obj.records || [];
        _this.pageTotal = obj.total;
      });
    },

    // 新增
    add(id) {
      this.$router.push({ name: "addArticle", query: { id: id } });
    },

    // 删除医生
    remove(row) {
      let _this = this;
      showModal({
        message: `确认删除${row.articleName}文章吗？`,
        showCancelButton: true,
        type:'warning',
        success: function () {
          let oParams = {};
          oParams.id = row.id;
          DELETE_ARTICLE_INFO(oParams).then((res) => {
            if (res.code == 200) {
              showToast({
                message: "删除成功",
                type: "success",
              });
              _this.getList();
            }
          });
        },
      });
    },

    // 上下架文章
    update(row) {
      let _this = this;
      let msg = '上架';
      if(row.isPublished){
        msg = '下架'
      }
      showModal({
        message: `确认${msg}文章吗？`,
        showCancelButton: true,
        type:'warning',
        success: function () {
          let oParams = {...row};
          oParams.isPublished = !row.isPublished;
          UPDATE_ARTICLE_INFO(oParams).then((res) => {
            if (res.code == 200) {
              showToast({
                message: "删除成功",
                type: "success",
              });
              _this.getList();
            }
          });
        },
      });
    },
  },
};
</script>
    
    <style lang="scss" scoped>
@import "@/assets/style/customer/index.scss";
</style>
    